var wxpay = require('../../utils/pay.js')
var app = getApp()
Page({
  data:{
    statusType: ["待报价", "我报的价", "已取消", "已完成"],
    currentType:0,
    tabClass: ["", "", "", "", ""],
    toastHidden:true,
    price:0,
    inquirySheetId:0,
    changeInquirySheetId:0
  },
  statusTap:function(e){
     var curType =  e.currentTarget.dataset.index;
     this.data.currentType = curType
     this.setData({
       currentType:curType
     });
     this.onShow();
  },
  orderDetail : function (e) {
    var orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/order-details/index?id=" + orderId
    })
  },
  updateQuote:function(e){
    wx.showLoading();
    var that = this;
    var inquiryId = e.currentTarget.dataset.id;
    wx.request({
      url: app.globalData._path + app.globalData.subDomain + '/inquiry/cancle',
      method:'post',
      data:{
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
        inquiy_id: inquiryId,
      },
      success:function(res){
        wx.hideLoading();
        if (res.data.code == 0) {
          wx.showToast({
            title: '关闭成功',
          })
          that.onLoad();
        } else {
          wx.showModal({
            title: '失败',
            content: res.data.list,
            showCancel: false
          });
        }
      }
    })
  },
  toPayTap:function(e){
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    var money = e.currentTarget.dataset.money;
    var needScore = e.currentTarget.dataset.score;
    wx.request({
      url: app.globalData._path + app.globalData.subDomain + '/user/amount',
      data: {
        token: wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.code == 0) {
          // res.data.data.balance
          money = money - res.data.data.balance;
          if (res.data.data.score < needScore) {
            wx.showModal({
              title: '错误',
              content: '您的积分不足，无法支付',
              showCancel: false
            })
            return;
          }
          if (money <= 0) {
            // 直接使用余额支付
            wx.request({
              url: app.globalData._path + app.globalData.subDomain + '/order/pay',
              method:'POST',
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              data: {
                token: wx.getStorageSync('token'),
                orderId: orderId
              },
              success: function (res2) {
                that.onShow();
              }
            })
          } else {
            wxpay.wxpay(app, money, orderId, "/pages/order-list/index");
          }
        } else {
          wx.showModal({
            title: '错误',
            content: '无法获取用户资金信息',
            showCancel: false
          })
        }
      }
    })    
  },
  toQuote:function(e){
    var that = this;
    that.setData({
      inquirySheetId : e.currentTarget.dataset.id,
      toastHidden : false
    });
  },
  setValue:function(e){
    var that = this;
    that.setData({
      price:e.detail.value
    });
  },
  onLoad:function(options){
    // 生命周期函数--监听页面加载
   
  },
  onReady:function(){
    // 生命周期函数--监听页面初次渲染完成
 
  },
  onShow:function(){
    // 获取订单列表
    wx.showLoading();
    var that = this;
    var postData = {
      token: wx.getStorageSync('token'),
      uid : wx.getStorageSync('uid')
    };
    postData.status = that.data.currentType;
    // this.getOrderStatistics();
    wx.request({
      url: app.globalData._path + app.globalData.subDomain + '/quote/index',
      data: postData,
      success: (res) => {
        wx.hideLoading();
        if (res.data.code == 0) {
          that.setData({
            quoteList: res.data.list.data
          });
        } else {
          that.setData({
            quoteList: null
          });
        }
      }
    })
    
  },
  onHide:function(){
    // 生命周期函数--监听页面隐藏
 
  },
  onUnload:function(){
    // 生命周期函数--监听页面卸载
 
  },
  onPullDownRefresh: function() {
    // 页面相关事件处理函数--监听用户下拉动作
   
  },
  onReachBottom: function() {
    // 页面上拉触底事件的处理函数
  
  },
  cancelToShow:function() {
    this.setData({
      toastHidden: true
    })
  },
  confirmQuote:function() {
    if (this.data.price <= 0){
      wx.showModal({
        title:'错误提示',
        content:'报价必须大于0',
        showCancel:false
      });
      return;
    }

    if (this.data.inquirySheetId == 0){
      wx.showModal({
        title: '错误提示',
        content: '未找到报价单',
        showCancel: false
      });
      return;
    }
    wx.showLoading({
      title: '正在加载',
    })
    wx.request({
      url: app.globalData._path + app.globalData.subDomain +'/quote/store',
      method:'post',
      data:{
        inquiry_sheet_id: this.data.inquirySheetId,
        price: this.data.price,
        uid: wx.getStorageSync('uid')
      },
      success:function (res){
        wx.hideLoading();
        if(res.data.code == 0){
          wx.showModal({
            title: '消息提示',
            content: '报价成功',
            showCancel:false,
            success:function (){
              wx.redirectTo({
                url: '/pages/quote/index',
              });
            }
          }); 
        }else{
          wx.showModal({
            title: '消息提示',
            content: res.data.list,
            showCancel: false
          })
        }
      }
    })
  }
})
